from flask import Flask,request,render_template,session,redirect
# 调用自定义模块中的函数
from reserch import reserch4letters
# 商品数据
from data import data

app = Flask(__name__)
app.secret_key = 'secret-key'


# -------------------------首页-----------------------------

@app.route('/',methods=['GET'])
def hello_world():
    return render_template('hello.html')



# ----------------------名字全称拼写------------------------

# 名字填写页面
@app.route('/name',methods=['GET'])
def name():
    return render_template('name.html')

# 处理提交的名字
@app.route('/submit_name',methods=['POST'])
def submit_name():
    # 拼接完整名字
    firstName = request.form['firstName']
    lastName = request.form['lastName']
    fullName = lastName + firstName
    # 姓和名也传递，目的是回填信息，避免输入框为空
    return render_template('name.html', first_name=firstName,last_name=lastName,
    full_name=fullName)



# ----------------------元音词频统计------------------------

# 单词填写页面
@app.route('/word',methods=['GET'])
def word():
    return render_template('word.html')


# 处理提交的单词
@app.route('/submit_word',methods=['POST'])
def submit_word():
    word = request.form['word']
    vowels = request.form['vowels']
    found = reserch4letters(vowels,word)
    # 获取频次
    num = 0
    for i in found:
        num = found[i]
    return render_template('word.html', num=num,word=word,vowels=vowels)


# -------------------------购物车-----------------------------

# 购物车数据
cartList = []

# 路由：返回'商品'页面
@app.route('/goods')
def goods():
    # 从session中获取money
    money = session.get('money')
    return render_template('goods.html', goods_list = data.goods_list, total_money=money)

# 路由：返回'购物车'页面
@app.route('/cart', methods=['GET'])
def cart():
    # 有两个入口进购物车，这里指的是点击商品加入购物车
    if request.args.get('_id'):
        # 当前物品
        curGood = {}
        for i in data.goods_list:
            if i['_id'] == request.args.get('_id'):
                curGood = i
        if curGood not in cartList:
            cartList.append(curGood)
    return render_template('cart.html', cart_list = cartList)

# 路由：结算（购物车中的全部物品）
@app.route('/shop')
def shop():
    # 页面文字
    msg = ''
    # 剩余费用
    left_money = 0
    # 计算总费用
    sum = 0
    for i in cartList:
        sum = sum + i['price']
    if session.get('money') == None:
        msg = '请在商品页面填写账户余额~'
    else:
        if sum > session.get('money'):
            msg = '余额不足，请充值~'
        else:
            msg = '购买成功，静待发货~'
            left_money = session.get('money') - sum
            # 将最新余额更新到session
            session['money'] = left_money
    return render_template('pay.html', msg=msg, left_money=left_money)

# 路由：购买单个物品页面
@app.route('/buy', methods=['GET'])
def buy():
    # 当前物品
    curGood = {}
    for i in data.goods_list:
        if i['_id'] == request.args.get('_id'):
            curGood = i
    return render_template('buy.html', cur_good = curGood)


# 路由：结算（单个物品）
@app.route('/buy_single')
def buy_single():
    # 页面文字
    msg = ''
    # 剩余费用
    left_money = 0
    # 单个物品费用
    price = 0
    for i in data.goods_list:
        if i['_id'] == request.args.get('_id'):
            price = i['price']
    if session.get('money') == None:
        msg = '请在商品页面填写账户余额~'
    else:
        if price > session.get('money'):
            msg = '余额不足，请充值~'
        else:
            msg = '购买成功，静待发货~'
            left_money = session.get('money') - price
            # 将最新余额更新到session
            session['money'] = left_money
    return render_template('pay.html', msg=msg, left_money=left_money)

# 路由：处理金额，存入session中
@app.route('/submit/money')
def submit_money():
    # 将金额存入session中，便于全局取用
    money = request.args.get('money')
    session['money'] = int(money)
    return redirect('/goods')


if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000,debug=True)